package com.ly.emp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.entity.HubTeamAdd;
import com.ly.emp.entity.HubTeamAddType;
import com.ly.emp.mapper.HubTeamAddMapper;
import com.ly.emp.service.IHubTeamAddService;
import com.ly.emp.service.IHubTeamAddTypeService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 一线班组-加减分指标管理-impl
 *
 * @author luoyun
 * @Date: 2025-06-20
 */
@Service
@RequiredArgsConstructor
public class HubTeamAddServiceImpl extends ServiceImpl<HubTeamAddMapper, HubTeamAdd> implements IHubTeamAddService {

    private final IHubTeamAddTypeService typeService;

    @Override
    public R getId(String id) {
        HubTeamAdd entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamAdd entity) {
        List<HubTeamAdd> list = this.list();
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamAdd entity, Page<HubTeamAdd> page) {
        IPage<HubTeamAdd> pageList = this.page(page,
                new LambdaQueryWrapper<HubTeamAdd>()
                        .eq(StringUtils.isNotBlank(entity.getOrgCode()), HubTeamAdd::getOrgCode, entity.getOrgCode())
                        .eq(StrUtil.isNotBlank(entity.getPeriodCode()), HubTeamAdd::getPeriodCode, entity.getPeriodCode())
                        .eq(StringUtils.isNotBlank(entity.getPeriodType()), HubTeamAdd::getPeriodType, entity.getPeriodType())
                        .like(StrUtil.isNotBlank(entity.getIndexName()), HubTeamAdd::getIndexName, entity.getIndexName())
                        .eq(StrUtil.isNotBlank(entity.getIndexTypeCode()), HubTeamAdd::getIndexTypeCode, entity.getIndexTypeCode())
                        .orderByAsc(HubTeamAdd::getDisplayOrder)
                        .orderByDesc(HubTeamAdd::getSysUpdateDate));
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamAdd entity) {
        List<HubTeamAdd> list = this.list(
                new LambdaQueryWrapper<HubTeamAdd>()
                        .eq(HubTeamAdd::getOrgCode, entity.getOrgCode())
                        .eq(HubTeamAdd::getPeriodCode, entity.getPeriodCode())
                        .eq(HubTeamAdd::getIndexName, entity.getIndexName())
                        .eq(HubTeamAdd::getIndexTypeName, entity.getIndexTypeName())
        );
        if (CollUtil.isNotEmpty(list)) {
            return R.fail("指标名称已存在！");
        }
        // 指标类型新版处理
        HubTeamAddType indexType = BeanUtil.copyProperties(entity, HubTeamAddType.class, new String[]{"seqKey"});
        String uidCode = typeService.getAndAdd(indexType);
        entity.setIndexTypeCode(uidCode);
        // 设置编码
        entity.setIndexCode(IdUtil.getSnowflakeNextIdStr());
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamAdd entity) {
        List<HubTeamAdd> list = this.list(
                new LambdaQueryWrapper<HubTeamAdd>()
                        .eq(HubTeamAdd::getOrgCode, entity.getOrgCode())
                        .eq(HubTeamAdd::getPeriodCode, entity.getPeriodCode())
        );
        HubTeamAdd index = list.stream()
                // 排除本身且判断指标名称和指标类型名称
                .filter(n -> !entity.getId().equals(n.getId())
                        && entity.getIndexName().equals(n.getIndexName())
                        && entity.getIndexTypeName().equals(n.getIndexTypeName()))
                .findAny().orElse(null);
        if (index != null) {
            return R.fail("指标名称已存在！");
        }
        // 指标类型新版处理
        HubTeamAddType indexType = BeanUtil.copyProperties(entity, HubTeamAddType.class, new String[]{"seqKey"});
        String uidCode = typeService.getAndAdd(indexType);
        entity.setIndexTypeCode(uidCode);
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamAdd> dto) {
        List<HubTeamAdd> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }
}
